<?php
/**
 * Description of UploadImageClass
 *
 * @author Chisheng Lu
 * @copyright DoRISE team, Science Education center, NTNU.
 * @since 2010-03-09
 * @version 1.0
 */
class UploadImage3 {
    //put your code here
    private $mysqli;

    function __construct() {
        //引入資料庫連結參數設定
        include '../inc/oaConn.inc.php';
        $this->mysqli = @new mysqli($mysqlHost,$mysqlUser,$mysqlPassword,$mysqlDB);
        if(mysqli_connect_errno()) {
            throw new Exception('無法建立資料庫連線！'.mysqli_connect_error());
            $this->mysqli = false;
            exit();
        }

        //連線使用編碼
        $this->mysqli->query("SET NAMES 'utf8'");
    }

    // $aryUploadImage: Array
    // Returns Boolean
    public function addUploadImage($aryUploadImage) {
        $bolRtn = false;
        $strNow = $this->getNowTime();
        $sql_addUI = 'INSERT INTO UploadImage (';
        $sql_values .= ' VALUES(';
        $bolFirst = true;
        if (array_key_exists('QuestionType', $aryUploadImage)) {
            if ($bolFirst == false) {
                $sql_addUI .= ', ';
                $sql_values .= ', ';
            }
            $sql_addUI .= 'QuestionType ';
            $sql_values .= $aryUploadImage['QuestionType'] . ' ';
            $bolFirst = false;
        }
        if (array_key_exists('QId', $aryUploadImage)) {
            if ($bolFirst == false) {
                $sql_addUI .= ', ';
                $sql_values .= ', ';
            }
            $sql_addUI .= 'QId ';
            $sql_values .= $aryUploadImage['QId'] . ' ';
            $bolFirst = false;
        }
        if (array_key_exists('FilePath', $aryUploadImage)) {
            if ($bolFirst == false) {
                $sql_addUI .= ', ';
                $sql_values .= ', ';
            }
            $sql_addMUI.= 'FilePath ';
            $sql_values .= '\'' . $aryUploadImage['FilePath'] . '\' ';
            $bolFirst = false;
        }
        $sql_values .=  ') ';
        $sql_addUI .= ')' . $sql_values . ';';
        $rs_addUI = $this->mysqli->query($sql_addUI);
        if (!$rs_addUI) {
            throw new Exception('新增上傳圖檔資料時發生錯誤！');
        }else{
            $bolRtn = true;
        }
        return $bolRtn;

    }

    // $intUIId: int
    // $aryUploadImage: Array
    // Returns Boolean
    public function editUploadImage($intUIId, $aryUploadImage) {
        //檢查題幹編號是否為空白
        if (empty ($intUIId)) {
            throw new Exception('缺少上傳圖檔編號！');
        }
        $bolRtn = false;
        $strNow = $this->getNowTime();
        $sql_editUI = 'Update UploadImage set ';
        $bolFirst = true;
        if (array_key_exists('QuestionType', $aryUploadImage)) {
             if ($bolFirst == false) {
                $sql_editUI .= ', ';
            }
            $sql_editUI .= 'Question = ' . $aryUploadImage['QuestionType'] . ' ';
            $bolFirst = false;
        }
        if (array_key_exists('QId', $aryUploadImage)) {
            if ($bolFirst == false) {
                $sql_editUI .= ', ';
            }
            $sql_editUI .= 'QId = ' . $aryUploadImage['QId'] . ' ';
            $bolFirst = false;
        }
        if (array_key_exists('FilePath', $aryUploadImage)) {
             if ($bolFirst == false) {
                $sql_editUI .= ', ';
            }
            $sql_editUI .= 'FilePath = \'' . $aryUploadImage['FilePath'] . '\' ';
            $bolFirst = false;
        }
        $rs_editUI = $this->mysqli->query($sql_editUI);
        if (!$rs_editUI) {
            throw new Exception('更新上傳圖檔資料時發生錯誤！');
        }else{
            $bolRtn = true;
        }
        return $bolRtn;
    }

    // $intUIId: int
    // Returns Boolean
    public function delUploadImage($intUIId) {
        $bolRtn = false;
        //檢查上傳圖檔編號是否為空白
        if (empty ($intUIId)) {
            throw new Exception('錯誤！缺少上傳圖檔編號。');
        }

        //刪除
        $sql_delUI = 'DELETE FROM `UploadImage` WHERE `UIId`=' . $intUIId;
        $rs_delUI = $this->mysqli->query($sql_delUI);
        if (!$rs_delUI) {
            throw new Exception('錯誤！刪除上傳圖檔時發生錯誤。');
        }else{
            $bolRtn = true;
        }

        return $bolRtn;

    }

    // $intUIId: int
    // Returns Array
    public function getUploadImageInfoByUIId($intUIId) {
        //檢查上傳圖檔編號是否為空白
        if (empty ($intUIId)) {
            throw new Exception('缺少上傳圖檔編號！');
        }

        $sql_getUIById = 'SELECT * FROM `UploadImage` WHERE `UIId`=' . $intUIId;
        $rs_getUIById = $this->mysqli->query($sql_getUIById);

        if (!$rs_getUIById) {
            throw new Exception('無法查詢該上傳圖檔的資料！');
        }else{
            if ($rs_getUIById->num_rows == 0) {
                throw new Exception('無此上傳圖檔編號的資料！');
            }else{
                $row_getUIById = $rs_getUIById->fetch_row();
                        return $row_getUIById;
            }
        } //End If

    }

    // Returns Array
    public function getUploadImageList() {
        $sql_getUIList = 'SELECT * FROM `UploadImage` ;';
        $rs_getUIList = $this->mysqli->query($sql_getUIList);

        if (!$rs_getUIList) {
            throw new Exception('無法查詢該上傳圖檔的資料！');
        }else{
            if ($rs_getUIList->num_rows == 0) {
                throw new Exception('無此上傳圖檔編號的資料！');
            }else{
                $aryUIList = array();
                while ($aryOneUI = $rs_getUIList->fetch_row()) {
                    array_push($aryUIList, $aryOneUI);
                }
                return $aryUIList;
            }
        } //End If

    }

    private function getNowTime() {
        return date('Y-m-d H:i:s');
    }

    function  __destruct() {
        if ($this->mysqli) {
            $this->mysqli->close();
            $this->mysqli = FALSE;
        }
    }
}
?>
